import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react-swc'
import tsconfigPaths from 'vite-tsconfig-paths'
import path from 'path'
import wasm from 'vite-plugin-wasm'
import compression from 'vite-plugin-compression'
// import { VitePWA } from 'vite-plugin-pwa'
import { visualizer } from 'rollup-plugin-visualizer'
// import cdnImport from 'vite-plugin-cdn-import'

export default defineConfig({
  build: {
    chunkSizeWarningLimit: 1000,
    rollupOptions: {
      treeshake: true,
      output: {
        manualChunks: {
          // 核心框架
          react: ['react', 'react-dom'],
          router: ['react-router-dom'],

          // 图表库（大文件）
          // echarts: ['echarts'],
          // 'echarts-wordcloud': ['echarts-wordcloud'],
          // UI 组件库
          // ui: ['@radix-ui/react-dialog', '@radix-ui/react-scroll-area', '@radix-ui/react-switch', '@radix-ui/react-toast', '@radix-ui/react-tooltip'],

          // 工具函数
          utils: ['clsx', 'tailwind-merge', 'class-variance-authority']

          // Markdown 相关（懒加载，只在需要时加载）
          // markdown: ['react-markdown', 'rehype-raw', 'remark-gfm', 'react-syntax-highlighter', 'react-copy-to-clipboard']
        }
      }
    },
    assetsDir: 'dist',
    sourcemap: false,
    minify: 'terser',
    terserOptions: {
      compress: {
        drop_console: true,
        drop_debugger: true
      }
    }
  },
  plugins: [
    react(),
    tsconfigPaths(),
    wasm(),
    compression({
      algorithm: 'gzip',
      ext: '.gz',
      deleteOriginFile: false
    }),
    compression({
      algorithm: 'brotliCompress',
      ext: '.br',
      deleteOriginFile: false
    }),
    // VitePWA({
    //   registerType: 'autoUpdate',
    //   workbox: {
    //     globPatterns: ['**/*.{js,css,html,ico,png,svg,wasm}'],
    //     runtimeCaching: [
    //       {
    //         urlPattern: /^https:\/\/pecs\.cn\//,
    //         handler: 'NetworkFirst',
    //         options: {
    //           cacheName: 'api-cache',
    //           cacheableResponse: {
    //             statuses: [0, 200]
    //           }
    //         }
    //       },
    //       {
    //         urlPattern: /^https:\/\/sun\.pecs\.cn\//,
    //         handler: 'NetworkFirst',
    //         options: {
    //           cacheName: 'api-cache',
    //           cacheableResponse: {
    //             statuses: [0, 200]
    //           }
    //         }
    //       }
    //     ]
    //   }
    // }),
    visualizer({
      filename: 'dist/bundle-analysis.html',
      open: false,
      gzipSize: true,
      brotliSize: true
    })
    // cdnImport({
    //   modules: [
    //     {
    //       name: 'echarts',
    //       var: 'echarts',
    //       path: 'https://cdn.bootcdn.net/ajax/libs/echarts/5.5.0/echarts.min.js'
    //     }
    //   ]
    // })
  ],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src')
    }
  },
  server: {
    open: true,
    port: 5173,
    proxy: {
      '/rpi': {
        target: 'https://pecs.cn',
        changeOrigin: true
      },
      '/file': {
        target: 'https://pecs.cn',
        changeOrigin: true
      },
      '/api': {
        target: 'https://pecs.cn',
        // // 如果是 /api/sun开头，替换成 /api/g/sun
        // rewrite: (path) => {
        //   if (/^\/api\/sun/.test(path)) {
        //     return path.replace(/^\/api\/sun/, '/g/sun');
        //   }
        //   return path.replace(/^\/api/, '');
        // },
        // target: 'http://localhost:8001',
        changeOrigin: true
      }
    },
    headers: {
      // 'Cache-Control': 'public, max-age=31536000, immutable'
    }
  }
  // test: {
  //   globals: true,
  //   environment: 'happy-dom',
  //   setupFiles: '.vitest/setup',
  //   include: ['**/test.{ts,tsx}']
  // }
})
